#======================================================================
# Product:      KnowledgeEngine (Plone Version)
#
# File:         BloggerAPI.py
#
# Copyright:    Copyright (c) 2006, Faulkner Technologies
#
# Author:       John Meredith
#
# Description:  BloggerAPI client.
#======================================================================


#======================================================================
# KnowledgeEngine Imports
#----------------------------------------------------------------------
from BloggerAPI import *

#======================================================================
# Zope Library Imports
#----------------------------------------------------------------------


#======================================================================
# Python Library Imports
#----------------------------------------------------------------------
import os
import sys
import unittest
from xmlrpclib import Fault


#======================================================================
# BloggerAPI blog client
#----------------------------------------------------------------------
class Wordpress(BloggerAPI):
    """
        A blog client that speaks the BloggerAPI. It's probably wise to
        use the AtomAPI if you are talking to Blogspot.com
    """

    # Some meta-data for the blog client
    TITLE = "Wordpress"
    
    #---------------------------------------------------------------------------
    # Flag to identify whether this API allows for anonymous posting to blogs
    ALLOW_ANONYMOUS = False


    def createPost(self, **args):
        """
            Makes a new post to a designated blog. Optionally, will publish the blog after making the post.
        """

        # Setup default newPost parameters
        config = {
            'publish' : True
        }
        config.update(args)

        # If we don't have a blogId, try and find it
        if not config.has_key('blogId') or not config['blogId']:
            # Use the first blog we find as a default
            userBlogsList = self.getUsersBlogs()
            config['blogID'] = userBlogsList[0]['blogid']


        # Check that we have some content
        if not config.has_key('content') and not config['content']:
            raise TypeError, "A blog post must contain some form of content/body text."

        content = config['content']
        
        # Check if we have any categories
        if config.has_key('category') and config['category']:
            content = "<category>%s</category>%s" % (config['category'], content, )

        # If we have a title, let's try and include it
        if config.has_key('title') and config['title']:
            content = "<title>%s</title>%s" % (config['title'], content, )

        # Now do the magic to create our post
        server = self.getServer()
        postId = server.blogger.newPost(
            self._APPKEY,
            str(config['blogId']),
            str(self.username),
            str(self.password),
            str(content),
            bool(config['publish'])
        )

        return postId


#======================================================================
# Register Blog client with the action
#----------------------------------------------------------------------
from Products.KnowledgeEngine.Actions.BlogPost.Clients import registerBlogClient
registerBlogClient(Wordpress)
